package com.example.dell.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dell.basicapplication.R;
import com.example.dell.interfacepack.BackCall;

public class DialogUtil {

    //常规但按钮选择框,需要最终调用show()方法
    public static Dialog showSingleButton(Context mContext, String title, String content, String confirm, final BackCall backCall){

        final Dialog dialog = new Dialog(mContext, R.style.dialogShowStyle);
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_single_layout, null);
        TextView mTitle = (TextView)layout.findViewById(R.id.title);
        TextView mContent = (TextView)layout.findViewById(R.id.content);
        TextView mConfirm = (TextView)layout.findViewById(R.id.confirm);

        //进行赋值操作
        mTitle.setText(title+"");
        mContent.setText(content+"");
        mConfirm.setText(confirm+"");

        //点击确定按钮
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backCall.deal(R.id.confirm,0);
                dialog.dismiss();
            }
        });

        Window w = dialog.getWindow();
        //只有这样才能弹框
       if (Build.VERSION.SDK_INT>=26) {//8.0新特性
            w.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        }else{
            w.setType( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        dialog.onWindowAttributesChanged(lp);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(layout);
        dialog.setCancelable(true);

        return dialog;

    }

    //常规但按钮选择框,需要最终调用show()方法,0为确定，1为取消
    public static Dialog showSelectButton(Context mContext, String title, String content, String confirm,String cancle, final BackCall backCall){

        final Dialog dialog = new Dialog(mContext, R.style.dialogShowStyle);
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_select_layout, null);
        TextView mTitle = (TextView)layout.findViewById(R.id.title);
        TextView mContent = (TextView)layout.findViewById(R.id.content);
        TextView mConfirm = (TextView)layout.findViewById(R.id.confirm);
        TextView mCancle= (TextView)layout.findViewById(R.id.cancel);

        //进行赋值操作
        mTitle.setText(title+"");
        mContent.setText(content+"");
        mConfirm.setText(confirm+"");
        mCancle.setText(cancle+"");

        //点击确定按钮
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backCall.deal(R.id.confirm,0);
                dialog.dismiss();
            }
        });
        mCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backCall.deal(R.id.confirm,1);
                dialog.dismiss();
            }
        });

        Window w = dialog.getWindow();
        //只有这样才能弹框
        if (Build.VERSION.SDK_INT>=26) {//8.0新特性
            w.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        }else{
            w.setType( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        dialog.onWindowAttributesChanged(lp);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(layout);
        dialog.setCancelable(true);
        return dialog;

    }


}
